﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for UpgradeDeploymentWindow.xaml
    /// </summary>
    public partial class UpgradeDeploymentWindow : Window
    {

        ServiceController.ServiceController serviceController;
        Entities.DeploymentProperty deploymentProperty;
        String ServiceName;
        private Storyboard startbusy;
        private Storyboard stopbusy;
       
      

        public UpgradeDeploymentWindow(Entities.DeploymentProperty _deploymentProperty,String _ServiceName)
        {
           
            InitializeComponent();
            ServiceName = _ServiceName;
            deploymentProperty = _deploymentProperty;
            TextBoxPkgUrl.Focus();
            TextBlockTitleName.Text = "Upgrade "+deploymentProperty.DeploymentSlot+" Deployment";
            serviceController = new ServiceController.ServiceController();
            serviceController.UpgradeDeploymentComplete += new ServiceController.ServiceControllerEventHandler(serviceController_UpgradeDeploymentComplete);
            startbusy = (Storyboard)this.FindResource("StartBusy");
            stopbusy = (Storyboard)this.FindResource("StopBusy");
        }

        void serviceController_UpgradeDeploymentComplete(object sender, ServiceController.ServiceControllerEventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
            {
                HideMask();
            if (!e.IsOperationSuccessful)
            {
                MessageBox.Show(e.error.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
            }
            else 
            {
                MessageBox.Show("Deployment upgraded successfully ");
                this.DialogResult = true;
                this.Close();
               

            }
            }));

        }

        private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Title = "Select a configuration file";
            OpenFiledialog.Filter = "Configuration files (*.cscfg)|*.cscfg|All files (*.*)|*.*";

            if ((bool)OpenFiledialog.ShowDialog())
            {
                TextBoxConfigFileLocation.Text = OpenFiledialog.FileName.ToString();
            }
        }

        private void BtnDeploy_Click(object sender, RoutedEventArgs e)
        {
            if (checkValidation())
            {
                ShowMask();
                serviceController.UpgradeDeploymentAsync(App.SubscriptionID, App.CertificatePath, ServiceName, deploymentProperty.DeploymentSlot, "Auto", TextBoxPkgUrl.Text, TextBoxConfigFileLocation.Text, TextBoxLabel.Text, String.Empty,null);
            }

        }

        private bool checkValidation()
        {

            // if(String.IsNullOrEmpty(TextBoxDeploymentName.Text))
            //{
            //    MessageBox.Show("Deployment name cannot be empty.");
            //    return false;
            //}

            if (String.IsNullOrEmpty(TextBoxPkgUrl.Text))
            {
                MessageBox.Show("Application Package Url cannot be empty.","Error",MessageBoxButton.OK,MessageBoxImage.Error);
                TextBoxPkgUrl.Focus();
                return false;
            }


            if (String.IsNullOrEmpty(TextBoxConfigFileLocation.Text))
            {
                MessageBox.Show("Configuration file Location cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxConfigFileLocation.Focus();
                return false;
            }
           
            if (String.IsNullOrEmpty(TextBoxLabel.Text))
            {
                MessageBox.Show("Label cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxLabel.Focus();
                return false;
            }
            
            return true;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }
        private void HideMask()
        {
            startbusy.Stop();
            stopbusy.Begin();
            border2_Copy.Visibility = Visibility.Collapsed;
        }

        private void ShowMask()
        {
            border2_Copy.Visibility = Visibility.Visible;
            startbusy.Begin();
        }

       

        private void HandleRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            string navigateUri = hl.NavigateUri.ToString(); 
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true; 
        }

        

        
    }
}
